home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_08 / 1108062a < prev    next >
Text File  |  1993-06-06  |  5KB  |  148 lines

  1.      /*******************************************
  2.      *
  3.      *   erosion(...
  4.      *
  5.      *   This function performs the erosion
  6.      *   operation.  If a value pixel has more
  7.      *   than the threshold number of 0
  8.      *   neighbors, you erode it by setting it
  9.      *   to 0.
  10.      *
  11.      *******************************************/
  12.  
  13. erosion(in_name, out_name, the_image, out_image,
  14.         il, ie, ll, le, value, threshold)
  15.    char   in_name[], out_name[];
  16.    int    il, ie, ll, le;
  17.    short  the_image[ROWS][COLS],
  18.           out_image[ROWS][COLS],
  19.           threshold, value;
  20. {
  21.    int    a, b, count, i, j, k;
  22.    int    length, width;
  23.    struct tiff_header_struct image_header;
  24.  
  25.    if(does_not_exist(out_name)){
  26.       printf("\n\n output file does not exist %s", out_name);
  27.       read_tiff_header(in_name, &image_header);
  28.       round_off_image_size(&image_header,
  29.                            &length, &width);
  30.       image_header.image_length = length*ROWS;
  31.       image_header.image_width  = width*COLS;
  32.       create_allocate_tiff_file(out_name, &image_header,
  33.                                 out_image);
  34.    }  /* ends if does_not_exist */
  35.  
  36.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  37.  
  38.       /***************************
  39.       *
  40.       *   Loop over image array
  41.       *
  42.       ****************************/
  43.  
  44.    for(i=0; i<ROWS; i++)
  45.       for(j=0; j<COLS; j++)
  46.          out_image[i][j] = the_image[i][j];
  47.  
  48.    printf("\n");
  49.    for(i=1; i<ROWS-1; i++){
  50.       if( (i%10) == 0) printf("%3d", i);
  51.       for(j=1; j<COLS-1; j++){
  52.          if(the_image[i][j] == value){
  53.             count = 0;
  54.             for(a=-1; a<=1; a++){
  55.                 for(b=-1; b<=1; b++){
  56.                       if(the_image[i+a][j+b] == 0)
  57.                          count++;
  58.                 }  /*  ends loop over b */
  59.             }  /* ends loop over a */
  60.             if(count > threshold) out_image[i][j] = 0;
  61.          }  /* ends if the_image == value */
  62.       }  /* ends loop over j */
  63.    }  /* ends loop over i */
  64.  
  65.    fix_edges(out_image, 3);
  66.  
  67.    write_array_into_tiff_image(out_name, out_image,
  68.                                il, ie, ll, le);
  69.  
  70. }  /* ends erosion */
  71.  
  72.  
  73.  
  74.  
  75.  
  76.      /*******************************************
  77.      *
  78.      *   dilation(...
  79.      *
  80.      *   This function performs the dilation
  81.      *   operation.  If a 0 pixel has more than
  82.      *   threshold number of value neighbors,
  83.      *   you dilate it by setting it to value.
  84.      *
  85.      *******************************************/
  86.  
  87. dilation(in_name, out_name, the_image, out_image,
  88.          il, ie, ll, le, value, threshold)
  89.    char   in_name[], out_name[];
  90.    int    il, ie, ll, le;
  91.    short  the_image[ROWS][COLS],
  92.           out_image[ROWS][COLS],
  93.           threshold, value;
  94. {
  95.    int    a, b, count, i, j, k;
  96.    int    length, width;
  97.    struct tiff_header_struct image_header;
  98.  
  99.    if(does_not_exist(out_name)){
  100.       printf("\n\n output file does not exist %s", out_name);
  101.       read_tiff_header(in_name, &image_header);
  102.       round_off_image_size(&image_header,
  103.                            &length, &width);
  104.       image_header.image_length = length*ROWS;
  105.       image_header.image_width  = width*COLS;
  106.       create_allocate_tiff_file(out_name, &image_header,
  107.                                 out_image);
  108.    }  /* ends if does_not_exist */
  109.  
  110.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  111.  
  112.       /***************************
  113.       *
  114.       *   Loop over image array
  115.       *
  116.       ****************************/
  117.  
  118.    printf("\n");
  119.  
  120.    for(i=1; i<ROWS-1; i++){
  121.       if( (i%10) == 0) printf("%3d", i);
  122.       for(j=1; j<COLS-1; j++){
  123.          out_image[i][j] = the_image[i][j];
  124.          if(the_image[i][j] == 0){
  125.             count = 0;
  126.             for(a=-1; a<=1; a++){
  127.                 for(b=-1; b<=1; b++){
  128.                    if(a!=0  &&  b!=0){
  129.                       if(the_image[i+a][j+b] == value)
  130.                          count++;
  131.                    }  /* ends avoid the center pixel */
  132.                 }  /*  ends loop over b */
  133.             }  /* ends loop over a */
  134.             if(count > threshold)
  135.                out_image[i][j] = value;
  136.          }  /* ends if the_image == 0 */
  137.       }  /* ends loop over j */
  138.    }  /* ends loop over i */
  139.  
  140.    fix_edges(out_image, 3);
  141.  
  142.    write_array_into_tiff_image(out_name, out_image,
  143.                                il, ie, ll, le);
  144.  
  145. }  /* ends dilation */
  146.  
  147.  
  148.